/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package station3d;

import static javax.media.opengl.GL.*;

import java.awt.*;
import java.awt.event.*;
import javax.media.opengl.*;
import javax.media.opengl.glu.GLU;
import com.sun.opengl.util.j2d.TextRenderer;

/**
 *
 * @author Martin
 */
public class JoglTest extends Frame implements GLEventListener, KeyListener, WindowListener {

    private static final int WIDTH = 800;
    private static final int HEIGHT = 600;

    private GLCanvas glCanvas;

    private volatile boolean running;

    private TextRenderer textRenderer;

    /** Inicializace okna a posluchačů událostí. */
    public JoglTest() {
        super("Jogl test");

        glCanvas = new GLCanvas();
        glCanvas.setPreferredSize(new Dimension(WIDTH, HEIGHT));
        glCanvas.addGLEventListener(this);

        this.addKeyListener(this);
        this.addWindowListener(this);

        this.setResizable(false);

        this.add(glCanvas);
        this.pack();
        this.setVisible(true);
    }

    /** Hlavní smyčka JOGL aplikace. */
    public void run() {
        running = true;

        while (running) {
            glCanvas.display();

            try {
                Thread.sleep(100);
            }
            catch (InterruptedException e) {}
        }

        this.dispose();
        System.exit(0);
    }

    public void init(GLAutoDrawable drawable) {
        GL gl = drawable.getGL();
        gl.glClearColor(0, 0, 0, 0);

        textRenderer = new TextRenderer(new Font("Verdana", Font.BOLD, 12));
    }

    public void display(GLAutoDrawable drawable) {
        GL gl = drawable.getGL();
        GLU glu = new GLU();

        gl.glViewport(0, 0, WIDTH, HEIGHT);

        gl.glMatrixMode(GL_PROJECTION);
        gl.glLoadIdentity();
        glu.gluOrtho2D(0, WIDTH, HEIGHT, 0);

        gl.glMatrixMode(GL_MODELVIEW);
        gl.glLoadIdentity();

        gl.glClear(GL_COLOR_BUFFER_BIT);

        textRenderer.beginRendering(WIDTH, HEIGHT);
        textRenderer.setColor(Color.WHITE);
        textRenderer.draw("Ukončíte stiskem klávesy [ESC].", 50, HEIGHT - 50);
        textRenderer.draw("Jogl test!",
                WIDTH - (int)textRenderer.getBounds("Jogl test!").getWidth() - 50,
                50);
        textRenderer.endRendering();


        gl.glBegin(GL_QUADS);

        gl.glColor3f(1, 0, 0);
        gl.glVertex2i(100, 100);
        gl.glColor3f(0, 1, 0);
        gl.glVertex2i(700, 100);
        gl.glColor3f(1, 1, 0);
        gl.glVertex2i(700, 500);
        gl.glColor3f(0, 0, 1);
        gl.glVertex2i(100, 500);

        gl.glEnd();
    }

    public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) {
        //throw new UnsupportedOperationException("Not supported yet.");
    }

    public void displayChanged(GLAutoDrawable drawable, boolean modeChanged, boolean deviceChanged) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void keyTyped(KeyEvent e) {
        //throw new UnsupportedOperationException("Not supported yet.");
    }

    public void keyPressed(KeyEvent e) {
        if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {
            running = false;
        }
    }

    public void keyReleased(KeyEvent e) {
        //throw new UnsupportedOperationException("Not supported yet.");
    }

    public void windowOpened(WindowEvent e) {
        //throw new UnsupportedOperationException("Not supported yet.");
    }

    public void windowClosing(WindowEvent e) {
        running = false;
    }

    public void windowClosed(WindowEvent e) {
        //throw new UnsupportedOperationException("Not supported yet.");
    }

    public void windowIconified(WindowEvent e) {
        //throw new UnsupportedOperationException("Not supported yet.");
    }

    public void windowDeiconified(WindowEvent e) {
        //throw new UnsupportedOperationException("Not supported yet.");
    }

    public void windowActivated(WindowEvent e) {
        //throw new UnsupportedOperationException("Not supported yet.");
    }

    public void windowDeactivated(WindowEvent e) {
        //throw new UnsupportedOperationException("Not supported yet.");
    }

}
